package com.web.netalbum.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.web.netalbum.bean.Album;
import com.web.netalbum.bean.Photo;
import com.web.netalbum.bean.User;
import com.web.netalbum.service.AlbumSevice;
import com.web.netalbum.service.PhotoService;
import com.web.netalbum.service.UserService;
import com.web.netalbum.service.impl.AlbumSeviceImpl;
import com.web.netalbum.service.impl.PhotoServiceImpl;
import com.web.netalbum.service.impl.UserServiceImpl;
import com.web.netalbum.util.EmailThread;
import com.web.netalbum.util.MD5Util;

public class WebUtil {
	/**
	 * 初始化主页
	 * @param request
	 * @param response
	 */
	private static AlbumSevice as=new AlbumSeviceImpl();
	private static PhotoService ps=new PhotoServiceImpl();
	private static UserService us=new UserServiceImpl();
	public static void initIndexBody(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		//1.先随机的找到四个相册
		List<Integer> list=new ArrayList<>();
		Random random=new Random();
		while(true){
			if(list.size()==4){
				break;
			}
			int num=random.nextInt(24);
			if(!list.contains(num)){
				list.add(num);
			}
		}
		List<Album> albums=as.findAllAlbum();
		Map<String,List<Photo>> map=new LinkedHashMap<String, List<Photo>>();
		for (int i = 0; i < list.size(); i++) {
			Album album = albums.get(list.get(i));
			int id=album.getId();
			String name=album.getName();
			//2.在每个相册中找到最近时间的六个相片
			List<Photo> photos=ps.findPhotosByAlbumId(id,5);
			map.put(name, photos);
		}

		//3.产生json字符串
		System.out.println(map.keySet().size());
		JsonConfig config=new JsonConfig();
		config.setExcludes(new String[]{"contentTime"});
		JSONObject jsonObject=JSONObject.fromObject(map,config);
	//	System.out.println(jsonObject);
		//4.写回
		response.getWriter().write(jsonObject.toString());


	}

	public static void initIndexCategroy(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		//1.查询所有的相册名和id
		List<Album> findAllAlbum = as.findAllAlbum();
		//2.产生json字符串
		JsonConfig config=new JsonConfig();
		config.setExcludes(new String[]{"description","open","contentTime","uid"});
		JSONArray jsonOArray=JSONArray.fromObject(findAllAlbum,config);
	//	System.out.println(jsonOArray.toString());
		//3.写回
		response.getWriter().write(jsonOArray.toString());
	}

	public static void checkUserName(String value, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		if(us.isExist(value)){
			response.getWriter().write("userName is existed");
		}else{
			response.getWriter().write("ok");
		}

	}

	public static void register(String userName, String email, String password,
			HttpServletRequest request, HttpServletResponse response) throws IOException {

		User user=new User();
		String id=UUID.randomUUID().toString();
		user.setId(id);
		user.setName(userName);
		user.setActived(false);
		user.setEmail(email);
		user.setPassword(password);
		Random random=new Random();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<4;i++){
			sb.append(random.nextInt(10));
		}
		user.setCode(sb.toString());
		System.out.println(user.getCode());
		us.register(user);
		EmailThread etEmailThread=new EmailThread(user,EmailThread.ACCOUNT_ACTIVED );
		etEmailThread.start();
		response.getWriter().write("register success,email has been send");

	}

	public static void activeCount(String id, String code,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		if(us.active(id, code)){
			response.sendRedirect("message.html");
		}else{
			response.sendRedirect("error.html");
		}
	}
	/**
	 * 调用service层的方法，根据返回值
	 * 若返回值为null，用户名或密码错误
	 * 若返回值不为null,判断user的actived属性
	 * 若为true，则登录成功
	 * 若为false，则用户没有激活
	 * 最后向前台输出一个json字符串
	 * 要有success和tip 两个属性
	 * success 放一个bool值
	 * tip 放出错信息 若没有 则输出一个空字符串
	 * @param userName
	 * @param password
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void login(String userName, String password,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		User user=us.login(userName, password);
		String str="";
		if(user==null){
			str="{\"success\":false,\"tip\":\"the username or password is wrong\"}";
			response.getWriter().write(str);
			return;
		}else{
			if(!user.isActived()){
				str="{\"success\":false,\"tip\":\"the username is not actived\"}";
				response.getWriter().write(str);
				return;
			}
			str="{\"success\":true,\"tip\":\"success\"}";
			response.getWriter().write(str);
		}
		
	}

	/**
	 * 根据邮箱去判断用户是否存在
	 * 调用service层向返回User对象
	 * 若user为null，则说明邮箱无效
	 * 最终返回的json字符串
	 * 包含success tip 两个属性
	 * @param email
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void resetPassword(String email, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		User user=us.existByEmail(email);
		String str="";
		if(user==null){
			str="{\"success\":false,\"tip\":\"the email is invalid\"}";
			response.getWriter().write(str);
			return;
		}else {
			str="{\"success\":true,\"tip\":\"\"}";
			response.getWriter().write(str);
		}
		
	}
	
	/**
	 *发送重置密码的邮件
	 *现根据email找到用户
	 *重新set其code属性
	 *然后再更新用户
	 *发送邮件
	 *最后返回一个发送成功的文本给前台页面
	 * @param email
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void sendResetPassEmail(String email,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		User user=us.existByEmail(email);
		String str="";
		if(user==null){
			str="{\"success\":false,\"tip\":\"the email is invalid\"}";
			response.getWriter().write(str);
			return;
		}else{
			Random random=new Random();
			StringBuilder sb=new StringBuilder();
			for(int i=0;i<4;i++){
				sb.append(random.nextInt(10));
			}
			user.setCode(sb.toString());
			System.out.println(user.getCode());
			us.updateUser(user);
			EmailThread etEmailThread=new EmailThread(user,EmailThread.RESET_PASSWORD);
			etEmailThread.start();
			response.getWriter().write("send success,email has been send");
		}
		
	}
	/**
	 * 根据id去查找用户，
	 * 匹配code值是否一致，
	 * 若一致则跳转到密码修改的页面 password页面
	 * String script="<script>$(document).ready(function(){$('.password').append('<input type='hidden' id='userid' value="+id+">');});</script>";
	 * 在跳转前先输出以上脚本
	 * 若不一致，则跳转到错误页面输出链接失效等信息
	 * @param id
	 * @param code
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void passReset(String id, String code,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		User user=us.findUser(id);
		if(code.equals(user.getCode())){
			Cookie cookie=new Cookie("resetUserId", user.getId());
			cookie.setMaxAge(600);
			response.addCookie(cookie);
			response.sendRedirect("password.html");
		}else{
			response.sendRedirect("error.html");
		}
	}
	/**
	 * 更新密码业务
	 * 1.先根据id找到用户
	 * 2.将密码加密之后，再设置给user
	 * 3.更新用户
	 * @param password
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void updatePassword(String id,String password,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		User user=us.findUser(id);
		if(user!=null){
			user.setPassword(MD5Util.md5Encode(password));
			us.updateUser(user);
			response.getWriter().write("change success");
		}else{
			response.getWriter().write("url is invaild");
		}
	}
	/**
	 * 根据关键字去搜索相片
	 * 在数据层匹配时 要用模糊查询
	 * 得到的是list集合，若list的集合的数据大于36则，只要只取前36个，将整个list集合放到内存中，以便在继续取
	 * 若小于等于36个，则全部要，
	 * 然后将list集合转成json字符串，然后传给前台
	 * @param key
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void search(String key, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Set<Photo> photos=ps.search(key);
		List<Photo> pList=new ArrayList<>();
		HttpSession session = request.getSession();
		if(photos.size()>36){
			int n=photos.size()/36;
			int m=photos.size()%36;
			if(m>0){
				n=n+1;
			}
			Cookie c1=new Cookie("pageNum", n+"");
			c1.setMaxAge(30*60);
			response.addCookie(c1);
			Cookie c2=new Cookie("page", "1");
			c2.setMaxAge(30*60);
			response.addCookie(c2);
			Iterator<Photo> iterator = photos.iterator();
			int count=0;
			while(iterator.hasNext()){
				pList.add(iterator.next());
				count++;
				if(count==36){
					break;
				}
			}
		}else{
			Iterator<Photo> iterator = photos.iterator();
			while(iterator.hasNext()){
				pList.add(iterator.next());
			}
		}
		List<Photo> photoList=new ArrayList<>(photos);
		session.setAttribute("photos", photoList);
		JsonConfig config=new JsonConfig();
		config.setExcludes(new String[]{"contentTime","uid","aid"});
		JSONArray array=JSONArray.fromObject(pList, config);
		response.getWriter().write(array.toString());
	}
	/**
	 * 根据相册id去查找图片
	 * 得到的是list集合，若list的集合的数据大于36则，只要只取前36个，将整个list集合放到内存中，以便在继续取
	 * 若小于等于36个，则全部要，
	 * 然后将list集合转成json字符串，然后传给前台
	 * @param albumId
	 * @param request
	 * @param response
	 */
	public static void album(String albumId, HttpServletRequest request,
			HttpServletResponse response) {
		try{
			int id=Integer.parseInt(albumId);
			List<Photo> photos=ps.findPhotosByAlbumId(id);
			List<Photo> pList=new ArrayList<>();
			HttpSession session = request.getSession();
			if(photos.size()>36){
				int n=photos.size()/36;
				int m=photos.size()%36;
				if(m>0){
					n=n+1;
				}
				Cookie c1=new Cookie("pageNum", n+"");
				c1.setMaxAge(30*60);
				response.addCookie(c1);
				Cookie c2=new Cookie("page", "1");
				c2.setMaxAge(30*60);
				response.addCookie(c2);
				Iterator<Photo> iterator = photos.iterator();
				int count=0;
				while(iterator.hasNext()){
					pList.add(iterator.next());
					count++;
					if(count==36){
						break;
					}
				}
			}else{
				Iterator<Photo> iterator = photos.iterator();
				while(iterator.hasNext()){
					pList.add(iterator.next());
				}
			}
			session.setAttribute("photos", photos);
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"contentTime","uid","aid"});
			JSONArray array=JSONArray.fromObject(pList, config);
			response.getWriter().write(array.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	/**
	 * 分页的业务
	 * 1.根据获取的page 获取相应photoList
	 * 2.将当前的页写到cookie中
	 * 3.转换成json字符串
	 * @param page
	 * @param pageNum
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("unchecked")
	public static void page(String page, String pageNum,HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession();
		List<Photo> photos=(List<Photo>) session.getAttribute("photos");
		if(photos!=null&&photos.size()>0){
			try{
				int p=Integer.parseInt(page);
				int pNum=Integer.parseInt(pageNum);
				List<Photo> tagetList=new ArrayList<>();
				if(p==pNum){
					for(int i=(p-1)*36;i<photos.size();i++){
						tagetList.add(photos.get(i));
					}
				}else{
					for (int i = (p-1)*36; i < 36*p; i++) {
						tagetList.add(photos.get(i));
					}
				}
				Cookie c=new Cookie("page", p+"");
				response.addCookie(c);
				session.setAttribute("photos", photos);
				JsonConfig config=new JsonConfig();
				config.setExcludes(new String[]{"contentTime","uid","aid"});
				JSONArray array=JSONArray.fromObject(tagetList, config);
				response.getWriter().write(array.toString());
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}

	/**
	 * 1根据图片的id 去查找图片
	 * 2.将图片的每个字段都写进cookie中
	 * 3.response 返回即可
	 * @param photoId
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void single(String photoId,String num, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Object attribute = request.getSession().getAttribute("allImages");
		if(attribute!=null){
			request.getSession().removeAttribute("allImages");
		}
		Photo p=ps.findPhotoById(photoId);
		request.getSession().setAttribute("photo", p);
		Cookie c1=new Cookie("photoId", p.getId()+"");
//		System.out.println(p.getName());
		Cookie c2=new Cookie("name", p.getName());
		Cookie c3=new Cookie("path", p.getPath());
		Cookie c4=new Cookie("click", p.getClick()+"");
		Cookie c5=new Cookie("praise", p.getPraise()+"");
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		String dateString=dateFormat.format(p.getContentTime());
		Cookie c6=new Cookie("contentTime", dateString);
		Cookie c7=new Cookie("userName", us.findUser(p.getUid()).getName());
		Cookie c8=new Cookie("albumId", p.getAid()+"");
		Cookie c9=new Cookie("num", Integer.parseInt(num)+1+"");
		response.addCookie(c1);
		response.addCookie(c2);
		response.addCookie(c3);
		response.addCookie(c4);
		response.addCookie(c5);
		response.addCookie(c6);
		response.addCookie(c7);
		response.addCookie(c8);
		response.addCookie(c9);
		response.getWriter().write("success");
	}
	
	/**
	 * 完成show.html 的初始化工作
	 * 
	 * 1.在同一相册查找7幅图像，选取其中的6幅图像
	 * 2.转成json字符串
	 * @param userId
	 * @param albumId
	 * @param request
	 * @param response
	 */
	public static void singleInit(String photoId, String albumId,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int id=Integer.parseInt(albumId);
			List<Photo> photos = ps.findPhotosByAlbumId(id, 7);
			List<Photo> photoList=new ArrayList<>();
			for (Photo photo : photos) {
				if(!photoId.equals(""+photo.getId())){
					photoList.add(photo);
				}
				if(photoList.size()==6){
					break;
				}
			}
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"keyword","name","click","praise","contentTime","uid","aid"});
			JSONArray array=JSONArray.fromObject(photoList, config);
			response.getWriter().write(array.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 1.先判断内存中是否存在
	 * 2.判断内存中已存在的照片数
	 * 3.判断照片数是否大于200
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public static void initImages(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		Object attribute = session.getAttribute("allImages");
		if(attribute==null){
			List<Photo> photos= (List<Photo>) session.getAttribute("photos");
			if(photos!=null){
				if(photos.size()<150){
					Photo photo = (Photo) session.getAttribute("photo");
					photos.remove(photo);
					photos.add(0, photo);
					List<Photo> allImages=new ArrayList<>();
					allImages.addAll(photos);
					List<Photo> pts=ps.findPhotos(200);
					for (Photo photo2 : pts) {
						if (!allImages.contains(photo2)) {
							allImages.add(photo2);
						}
						if(allImages.size()>200){
							break;
						}
					}
					session.setAttribute("allImages", allImages);
					Cookie c=new Cookie("size", allImages.size()+"");
					response.addCookie(c);
				}else{
					Photo photo = (Photo) session.getAttribute("photo");
					photos.remove(photo);
					photos.add(0, photo);
					session.setAttribute("allImages", photos); 
					Cookie c=new Cookie("size", photos.size()+"");
					response.addCookie(c);
				}
			}else{
				List<Photo> pts=ps.findPhotos(200);
				Photo photo = (Photo) session.getAttribute("photo");
				pts.remove(photo);
				pts.add(0, photo);
				Cookie c=new Cookie("size", pts.size()+"");
				response.addCookie(c);
				session.setAttribute("allImages", pts);
			}
			
		}
		response.getWriter().write("images init...");
		
	}

	/**
	 * 当点击左侧边栏的时候
	 * @param num
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("unchecked")
	public static void left(String num, HttpServletRequest request,
			HttpServletResponse response) {
		try{
			int n=Integer.parseInt(num)-1;
			HttpSession session = request.getSession();
			List<Photo>  allImages= (List<Photo>) session.getAttribute("allImages");
			Photo p = allImages.get(n);
			session.setAttribute("photo", p);
			Cookie c1=new Cookie("photoId", p.getId()+"");
//			System.out.println(p.getName());
			Cookie c2=new Cookie("name", p.getName());
			Cookie c3=new Cookie("path", p.getPath());
			Cookie c4=new Cookie("click", p.getClick()+"");
			Cookie c5=new Cookie("praise", p.getPraise()+"");
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			String dateString=dateFormat.format(p.getContentTime());
			Cookie c6=new Cookie("contentTime", dateString);
			Cookie c7=new Cookie("userName", us.findUser(p.getUid()).getName());
			Cookie c8=new Cookie("albumId", p.getAid()+"");
			Cookie c9=new Cookie("num", n+"");
			response.addCookie(c1);
			response.addCookie(c2);
			response.addCookie(c3);
			response.addCookie(c4);
			response.addCookie(c5);
			response.addCookie(c6);
			response.addCookie(c7);
			response.addCookie(c8);
			response.addCookie(c9);
			
			List<Photo> photos = ps.findPhotosByAlbumId(p.getAid(), 7);
			List<Photo> photoList=new ArrayList<>();
			for (Photo photo : photos) {
				if(!(p.getId()==photo.getId())){
					photoList.add(photo);
				}
				if(photoList.size()==6){
					break;
				}
			}
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"keyword","name","click","praise","contentTime","uid","aid"});
			JSONArray array=JSONArray.fromObject(photoList, config);
			response.getWriter().write(array.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 当点击右侧边栏的时候
	 * @param num
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("unchecked")
	public static void right(String num, HttpServletRequest request,
			HttpServletResponse response) {
		try{
			int n=Integer.parseInt(num)+1;
			HttpSession session = request.getSession();
			List<Photo>  allImages= (List<Photo>) session.getAttribute("allImages");
			Photo p = allImages.get(n);
			session.setAttribute("photo", p);
			Cookie c1=new Cookie("photoId", p.getId()+"");
//			System.out.println(p.getName());
			Cookie c2=new Cookie("name", p.getName());
			Cookie c3=new Cookie("path", p.getPath());
			Cookie c4=new Cookie("click", p.getClick()+"");
			Cookie c5=new Cookie("praise", p.getPraise()+"");
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			String dateString=dateFormat.format(p.getContentTime());
			Cookie c6=new Cookie("contentTime", dateString);
			Cookie c7=new Cookie("userName", us.findUser(p.getUid()).getName());
			Cookie c8=new Cookie("albumId", p.getAid()+"");
			Cookie c9=new Cookie("num", n+"");
			response.addCookie(c1);
			response.addCookie(c2);
			response.addCookie(c3);
			response.addCookie(c4);
			response.addCookie(c5);
			response.addCookie(c6);
			response.addCookie(c7);
			response.addCookie(c8);
			response.addCookie(c9);
			
			List<Photo> photos = ps.findPhotosByAlbumId(p.getAid(), 7);
			List<Photo> photoList=new ArrayList<>();
			for (Photo photo : photos) {
				if(!(p.getId()==photo.getId())){
					photoList.add(photo);
				}
				if(photoList.size()==6){
					break;
				}
			}
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"keyword","name","click","praise","contentTime","uid","aid"});
			JSONArray array=JSONArray.fromObject(photoList, config);
			response.getWriter().write(array.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 * @param userName
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void person(String userName, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		User user = us.findUserByName(userName);
		PrintWriter writer = response.getWriter();
		HttpSession session = request.getSession();
		if(session.getAttribute("listPhotos")!=null){
			session.removeAttribute("listPhotos");
		}
		if(user!=null){
			List<Album> albums = as.findAllAlbum();
			Map<String, List<Photo>> mapPhotos=new HashMap<>();
			for (Album album : albums) {
				List<Photo> pList=ps.findPhotosByAidAndUid(album.getId(),user.getId());
				if(pList!=null && pList.size()>0){
					mapPhotos.put(album.getName(), pList);
				}
			}
			session.setAttribute("mapPhotos", mapPhotos);
			writer.write("{\"success\":true}");
		}else{
			writer.write("{\"success\":false}");
		}
		
	}

	/**
	 * 为个人页面初始化
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public static void personInit(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter writer = response.getWriter();
		String usreName = request.getParameter("userName");
		User user = us.findUserByName(usreName);
		if(user==null){
			writer.write("{\"success\":true}");
		}else{
			HttpSession session = request.getSession();
			Map<String, List<Photo>> mapPhotos = (Map<String, List<Photo>>) session.getAttribute("mapPhotos");
			if(mapPhotos!=null){
				List<Album> albums = as.findAllAlbum();
				StringBuilder sb=new StringBuilder();
				sb.append("[");
				int i=0;
				for (Album album : albums) {
					List<Photo> list = mapPhotos.get(album.getName());
					if(list!=null && list.size()>0){
						sb.append("{");
						sb.append("\""+"album\":\""+album.getName()+"\"");
						sb.append(",");
						sb.append("\""+"albumid\":\""+album.getId()+"\"");
						sb.append(",");
						sb.append("\"image\":\""+list.get(0).getPath()+"\"");
						sb.append(",");
						SimpleDateFormat  simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd天");
						String format = simpleDateFormat.format(album.getContentTime());
						sb.append("\"date\":"+"\""+format+"\"");
						sb.append("}");
						if(i!=albums.size()-1){
							sb.append(",");
						}
					}
					i++;
					
				}
				sb.append("]");
				writer.write(sb.toString());
			}else{
				List<Album> albums = as.findAllAlbum();
				Map<String, List<Photo>> mapPhotos1=new HashMap<>();
				for (Album album : albums) {
					List<Photo> pList=ps.findPhotosByAidAndUid(album.getId(),user.getId());
					if(pList!=null && pList.size()>0){
						mapPhotos1.put(album.getName(), pList);
					}
				}
				session.setAttribute("mapPhotos", mapPhotos1);
			}
		}
		
		
	}
	/**
	 * 展示所有的图片
	 * @param userName
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void showAllPhotos(String userName,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter writer = response.getWriter();
		User user = us.findUserByName(userName);
		if(user==null){
			writer.write("{\"success\":true}");
		}else{
			List<Photo> photos=ps.findAllPhotosByUserId(user.getId());
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"keyword","click","praise","contentTime","uid","aid"});
			JSONArray array=JSONArray.fromObject(photos, config);
			response.getWriter().write(array.toString());
		}
	}
}
